package org.spiderflow.core.expression.parsing.ast;

import org.spiderflow.core.expression.ExpressionTemplate;
import org.spiderflow.core.expression.ExpressionTemplateContext;
import org.spiderflow.core.expression.parsing.CharacterStream;
import org.spiderflow.core.expression.parsing.Span;

import java.io.IOException;

/**
 * A text node represents an "un-templated" span in the source that should be emitted verbatim.
 *
 * @author zhaoshihao
 * @version 2021/11/9 14:35
 **/
public class Text extends Node {
    private final String content;

    public Text(Span text) {
        super(text);
        String unescapedValue = text.getText();
        StringBuilder builder = new StringBuilder();

        CharacterStream stream = new CharacterStream(unescapedValue);
        while (stream.hasMore()) {
            if (stream.match("\\{", true)) {
                builder.append('{');
            } else if (stream.match("\\}", true)) {
                builder.append('}');
            } else {
                builder.append(stream.consume());
            }
        }
        content = builder.toString();
    }

    /**
     * Returns the UTF-8 representation of this text node.
     **/
    public String getContent() {
        return content;
    }

    @Override
    public Object evaluate(ExpressionTemplate template, ExpressionTemplateContext context) throws IOException {
        return null;
    }
}
